% To calculate the rating change time intervals

clc
clear all

conn = database('BDC','','');
labels = {1.5,2,2.5,3,3.5,4,4.5,-1};
year_list=1997:2009;
DURATION = 1;
NUMBER_OF_YEAR = length(year_list) - DURATION;
conditions = {'maxPPDOS_sum >= 50000','maxPPDOS_sum >= 50000 and maxPPDOS_sum < 250000',...
    'maxPPDOS_sum >= 250000 and maxPPDOS_sum <1000000','maxPPDOS_sum >= 1000000'};
numRate = length(labels);
numGrp = length(conditions);
SG = cellstr(char('Sample 1: [$50k,+)','Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)'));
startYear = input('Please enter a value for startYear:');
endYear = input('Please enter a value for endYear:');

Date1 = strcat(num2str(startYear),'-12-01');
Date2 = strcat(num2str(endYear),'-12-01');
disp( strcat(['Date Range: ' Date1 ' to ' Date2]));


cdIdx = 2: 2; %1:numGrp
condition = char(conditions(cdIdx));
sqlquery1 = strcat(['select Customer, modiFisDate, modiRate FROM final_v5_tbl where ' condition ' and modiFisDate BETWEEN ''' Date1 ''' and ''' Date2 ''' order by customer, modiFisDate limit 1000']);

results = fetch(conn, sqlquery1);
disp(size(results));

% l = 1;
% A = ones(length(label)-1,length(label));
path = Liklihood(results,'labels',labels);
load('D:\My Documents\CIRANO\db_mySQL\generatorAdj_2005.mat')
q = yyy;
%L = liklihoodFun(q,path,1);
% dL = liklihoodFun_dL(q,path);
    

q = qnewton('liklihoodFun',q,path);
    
   





